package tool;

import java.awt.HeadlessException;
import java.awt.TextField;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;

public class MyTextField extends TextField 
implements ISync,TextListener{

	private String id = "";
	private String content = "";
	
	public MyTextField() throws HeadlessException {
		super();
		addTextListener(this);
	}

	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void textValueChanged(TextEvent e) {
		if(content.equals(getText()))return;
		sync(SYNC_TO_DATA_OBJECT);
		MyPanel.instance.syncAll();
	}
	
	public void sync(int method) {
		switch(method)
		{
		case SYNC_FROM_DATA_OBJECT:syncFrom();break;
		case SYNC_TO_DATA_OBJECT:syncTo();break;
		}
	}
	
	private void syncFrom()
	{
		int current = MyPanel.instance.fileData.currentClipIndex;
		Clip c = MyPanel.instance.fileData.clips.getClipAt(current);
		setText(c.getName());
	}
	
	private void syncTo()
	{
		int current = MyPanel.instance.fileData.currentClipIndex;
		Clip c = MyPanel.instance.fileData.clips.getClipAt(current);		
		c.setName(getText());
		content = getText();
	}

}
